package hu.test.atacama;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Get_SensorData extends Activity {
	Button refreshButton, forcedMeasureButton;
	TextView textViewSensors;
	
	IntentFilter commServIntentFilter, commServErrorIntentFilter;
	CommServiceReceiver commServiceReceiver = new CommServiceReceiver();
	final Intent commServiceIntent = new Intent();
	Common common;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_get__sensor_data);
		
		common = Common.getInstance(this);
		commServiceIntent.setClass(getApplicationContext(), CommService.class);
		commServIntentFilter = new IntentFilter(CommService.COMMSERVICE_DONE);
		commServErrorIntentFilter = new IntentFilter(CommService.COMMSERVICE_ERROR);
		
		textViewSensors = (TextView) findViewById(R.id.textViewSensorDatas);
		refreshButton = (Button) findViewById(R.id.buttonRefresh);
		forcedMeasureButton = (Button) findViewById(R.id.buttonForcedMeasure);
		
		refreshButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSensorValues();
			}
		});
		
		forcedMeasureButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.get__sensor_data, menu);
		return true;
	}

	@Override
	protected void onPause() {
		unregisterReceiver(commServiceReceiver);
		unregisterReceiver(commServiceError);
		unregisterReceiver(sensorDatasDownloaded);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		getSensorValues();
		
		refreshTextFields();
		registerReceiver(commServiceReceiver, commServIntentFilter);
		registerReceiver(commServiceError, commServErrorIntentFilter);
		registerReceiver(sensorDatasDownloaded, commServIntentFilter);
	}

	void refreshTextFields() {
		
		String[] coordinates = common.getCarLastKnownCoordinates().split(",");
//		Double carLatitude = Double.parseDouble(coordinates[0]);
//		Double carLongitude = Double.parseDouble(coordinates[1]);
		String carLatitude = coordinates[0];
		String carLongitude = coordinates[1];
		String carLastSeen = coordinates[2]; 		
	
		StringBuilder builder = new StringBuilder();
		builder.append(getString(R.string.text_sensor_datas)
				+ System.getProperty("line.separator")
				+ System.getProperty("line.separator"));
		builder.append("Car: " + common.getCurrentCarName()
				+ System.getProperty("line.separator"));		
		builder.append("GPS latitude: " + carLatitude
				+ System.getProperty("line.separator"));
		builder.append("GPS longitude: " + carLongitude
				+ System.getProperty("line.separator"));		
		builder.append("Last seen at: " + carLastSeen				
				+ System.getProperty("line.separator"));
		
		for (Sensor sensor : common.sensorList) {
			builder.append(sensor.getSensorName() + ": " + sensor.getSensorValue() + " " + sensor.getUnitOfMeasure() + System.getProperty("line.separator"));
		}		
		builder.append(System.getProperty("line.separator"));
		
		textViewSensors.setText(builder);				
	}
	
	private BroadcastReceiver commServiceError = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			setProgressBarIndeterminateVisibility(false);
			common.displayToast("Commservice error");
		}
	};

	private BroadcastReceiver sensorDatasDownloaded = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			setProgressBarIndeterminateVisibility(false);
			refreshTextFields();			
		}
	};
	
	private void starCommService(DataFromDevice dataFromDevice,
			String startingMessage) {
		setProgressBarIndeterminateVisibility(true);
		commServiceIntent.putExtra("dataFromDevice", dataFromDevice);
		commServiceIntent.putExtra("startingMessage", startingMessage);

		// registerReceiver(commServiceReceiver, commServIntentFilter);
		// registerReceiver(commServiceDone, commServIntentFilter);
		startService(commServiceIntent);
	}
	
	private void getSensorValues() {
		Long timeStamp = (long) 0;
		String gpsData = "None";
		String metaData = common.getCurrentCarKey();
				
		DataFromDevice dataFromDevice = new DataFromDevice(common.getDeviceType(),
				common.getSenderID(), common.getSerialNumber(), gpsData, timeStamp,
				Common.COMMAND_GET_SENSOR_VALUES, metaData);

		starCommService(dataFromDevice,
				getString(R.string.message_get_sensor_datas));
	}
}
